const flvDataParse = {}
const utf8 = window.utf8


// flv tag type = 18
flvDataParse.parseObject = function (uint8a, dataOffset, dataSize) {}

// script data string
flvDataParse.parseString = function (uint8a, dataOffset, dataSize) {
    var len = (uint8a[dataOffset] << 8 ) | uint8a[dataOffset + 1]
    var string = utf8.decode(uint8a, dataOffset + 2, len)
    return {
        value: string,
        size: len + 2
    }
}

// script data long string
flvDataParse.parseLongString = function (uint8a, dataOffset, dataSize) {
    var len = 
        (uint8a[dataOffset] << 24) |
        (uint8a[dataOffset + 1] << 16) |
        (uint8a[dataOffset + 2] << 8) |
        (uint8a[dataOffset + 3])
    var string = utf8.decode(uint8a, dataOffset + 4, len)
    return {
        value: string,
        size: len + 2
    }
}

flvDataParse.parseValue = function (uint8a, dataOffset, dataSize) {}

// script data variable
flvDataParse.parseVariable = function (uint8a, dataOffset, dataSize) {
    var offset = dataOffset
    var variableName = flvDataParse.parseString(uint8a, offset, dataSize)

    offset += variableName.size
    var variableData = flvDataParse.parseValue(uint8a, offset, dataSize - variableName.size)

    return {
        value: {
            variableName,
            variableData
        },
        size: (variableName.size + variableData.size)
    }
}

// script data date
flvDataParse.parseDate = function (uint8a, dataOffset, dataSize) {
    var view = new DataView(uint8a, dataOffset, dataSize)
    var dateTime = view.getFloat64(0)
    var offset = view.getInt16(9)
    return {
        value: {
            dateTime,
            localeDateTimeOffset: offset
        },
        size: 10
    }
}

